package com.project.running.web.Servlet;

import com.project.running.Exception.CustomerException;
import com.project.running.Service.Impl.RegisterImpl;
import com.project.running.Service.registerService;
import com.project.running.bean.Memberinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/5-10-05-16:46
 * @Description：com.project.running.web.Servlet
 */

@WebServlet("/register")
@Slf4j                            //日志注解
@Controller
public class Register extends HttpServlet {

    @Autowired
    registerService service;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        Memberinfo member = new Memberinfo();

        String authCode = req.getParameter("authCode");
        String verifyCode = (String) req.getSession().getAttribute("verifyCode");     //验证码

        member.setNickname(req.getParameter("nickName"));      //昵称
        member.setPassword(req.getParameter("passwd"));         //密码
        member.setEmail(req.getParameter("email"));           //邮箱
        member.setPasswordquestion(req.getParameter("passwdQuestion"));           //问题
        member.setPasswordanswer(req.getParameter("passwdAnswer"));        //答案
        member.setGender(req.getParameter("gender"));                //性别
        member.setAge(Long.parseLong(req.getParameter("age")));          //年龄
        member.setRecommender(req.getParameter("recommender"));        //推荐人
        member.setProvincecity(req.getParameter("provinceCity"));       //省份
        member.setPhone(req.getParameter("phone"));                   //电话
        member.setAddress(req.getParameter("address"));                //地址
        member.setRegisterdate(new Date(System.currentTimeMillis()));      //注册时间
        member.setPoint(50L);                                      //积分： 注册默认获得50积分
        member.setStatus(0L);                                   //账号会员状态  0非会员
        member.setIsonline(0L);                                  //账号设置为下线状态
        member.setGradeid(1L);                             //刚注册的账号等级状态为1级

        String msg = service.register(member,authCode,verifyCode);

        log.debug(msg);

        if(msg.equals("注册成功")){
            resp.sendRedirect("login.jsp");
        }else{
            throw new CustomerException(msg);
        }
    }
}
